package com.jz.kufangkuqu.kuwei.service.impl;

import com.jz.beijianbao.beijian.personEnum.PersonEnum;
import com.jz.kufangkuqu.kuwei.bean.*;
import com.jz.kufangkuqu.kuwei.mapper.KuWeiMapper;
import com.jz.kufangkuqu.kuwei.service.KuWeiService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KuWeiServiceImpl implements KuWeiService {

    @Autowired
    private KuWeiMapper kuWeiMapper;


    @Override
    public ResultVO queryPage(KuWeiSelect kuWeiSelectVo) {
        List<KuWei> kuWeiList = kuWeiMapper.queryPage(kuWeiSelectVo);
        for (KuWei plant: kuWeiList) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        int count = kuWeiMapper.count(kuWeiSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(kuWeiSelectVo.getPageIndex());
        allRecordsVo.setPageSize(kuWeiSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(kuWeiList);
        return ResultBuildVO.success(allRecordsVo);
    }

    @Override
    public ResultVO add(KuWeiAdd kuWeiAdd) {
        Date now = new Date();
        KuWei legalPlantVo1 = new KuWei();
        BeanUtils.copyProperties(kuWeiAdd,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        kuWeiMapper.add(legalPlantVo1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO update(KuWeiUpdate kuWeiUpdate) {
        Date now = new Date();
        KuWei legalPlant1 = new KuWei();
        BeanUtils.copyProperties(kuWeiUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = kuWeiMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(KuWeiUpdatestatus kuWeiUpdatestatus) {
        Date now = new Date();
        KuWei legalPlant1 = new KuWei();
        BeanUtils.copyProperties(kuWeiUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        kuWeiMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();
    }



}
